package com.yiren.dbaa.module.device.dialog;

import android.app.Activity;

import com.yiren.backstage.communication.thread.RTKConnection;
import com.yiren.backstage.currency.constant.SignalGConstant;
import com.yiren.dbaa.constant.G;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.help_center.bean.DeviceUpdateBean;
import com.yiren.dbaa.view.dialog.CustomDialog;
import com.yiren.dbaa.view.dialog.LoadDialog;

import org.apache.commons.lang.StringUtils;

/**
 * Created by WANGRONGXIANG
 * on 2022/6/14
 * Describe 硬件更新对话框
 */
public class HardwareUpdateDialog {

    private Activity activity;

    public HardwareUpdateDialog(Activity activity) {
        this.activity = activity;
    }

    public void show(DeviceUpdateBean deviceUpdateBean, CommonCallBack callBack) {
        StringBuffer sb = new StringBuffer();
        sb.append("检测到新版本:");
        sb.append(deviceUpdateBean.getVersionname());
        sb.append(", 是否更新?\n");
        String updateContext = "";
        for (int i = 0; i < deviceUpdateBean.getContexts().size(); i++) {
            updateContext += deviceUpdateBean.getContexts().get(i) + "\n";
        }
        sb.append(updateContext);
        CustomDialog.Builder builder = new CustomDialog.Builder(activity);
        builder.setTitle("系统更新");
        builder.setMessage(sb.toString());
        builder.setCanceled(false);
        builder.setUpApk(true);
        builder.setPositiveButton("更新", (dialog, which) -> {
            dialog.dismiss();
            SignalGConstant.isObdUpdating = true;
            callBack.complete();
//            hardwareUpdatePresenter.updateHardwareVersion(deviceUpdateBean);
        });
        builder.setNegativeButton("暂不更新", (dialog, which) -> {
            dialog.dismiss();
            //TODO 检测软件更新
        });
        if (!activity.isFinishing()) builder.create().show();
    }

    /**
     * 显示更新失败对话框
     *
     * @param str
     */
    public void showUpdateFailDialog(String str) {
        SignalGConstant.isObdUpdating = false;
        CustomDialog.Builder builder = new CustomDialog.Builder(activity);
        String buttString;
        if (StringUtils.isNotEmpty(str)) {
            LoadDialog.dismiss(activity);
            buttString = "重新检测";
            builder.setMessage("主机升级失败，请检查串口线是否连接正常，！");
        } else {
            buttString = "我知道了";
            builder.setMessage("主机升级失败，可能会影响您的正常使用！");
        }
        builder.setTitle("升级失败");
        builder.setPositiveButton(buttString, (dialog, which) -> {
            if (StringUtils.isNotEmpty(str)) {

            }
            dialog.dismiss();
        });
        if (!activity.isFinishing()) builder.create().show();
    }

    /**
     * 显示主机程序更新成功弹框
     */
    public void showUpdateSuccessDialog(DeviceUpdateBean deviceUpdateBean) {
        CustomDialog.Builder builder = new CustomDialog.Builder(activity);
        builder.setMessage("主机程序更新成功!");
        builder.setTitle("提示");
        builder.setCanceled(false);
        builder.setPositiveButton("我知道了", (dialog, which) -> {
            RTKConnection.useCommand = false;
            if (G.connectDeviceMessage != null) {
                G.connectDeviceMessage.setVersionCodeByHost(deviceUpdateBean.getVersioncode());
                G.connectDeviceMessage.setVersionDescribe(deviceUpdateBean.getVersionname());
            }
            dialog.dismiss();
        });
        if (!activity.isFinishing()) builder.create().show();
    }
}
